package com.tiger.log.core.aop.business;

import com.tiger.log.common.entity.SysRecordLog;
import com.tiger.log.core.processing.business.FormProcessing;
import com.tiger.log.server.service.SysRecordLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @ClassName PostFormAop
 * @Description TODO
 * @Author huxuehao
 **/
@Aspect
@Component
public class FormAop {
    private final FormProcessing formProcessing;
    private final SysRecordLogService sysRecordLogService;

    public FormAop(FormProcessing formProcessing, SysRecordLogService sysRecordLogService) {
        this.formProcessing = formProcessing;
        this.sysRecordLogService = sysRecordLogService;
    }

    @Pointcut("@annotation(com.tiger.log.common.annotation.business.form.FormCollect)")
    public void postFromPointCut() {

    }

    @Around("postFromPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        if (formProcessing.isIllegal(joinPoint)) {
            return joinPoint.proceed();
        }
        SysRecordLog recordLog = new SysRecordLog();
        Object res;
        try {
            /* 执行前的处理 */
            formProcessing.proceedPreProcessing(recordLog, joinPoint);
            /* 执行 */
            res = joinPoint.proceed();
            /* 执行后的处理 */
            formProcessing.proceedPostProcessing(recordLog, res, false);
        } catch (Throwable e) {
            /* 出现异常时的后置处理 */
            formProcessing.proceedPostProcessing(recordLog, null, true);
            throw e;
        } finally {
            /* 异步日志生生成 */
            sysRecordLogService.asyncGenLog(recordLog);
        }
        return res;
    }
}
